//公共方法
Handlebars.registerHelper('if_eq', function(v1, v2, opts) {
    if(v1 == v2)
        return opts.fn(this);
    else
        return opts.inverse(this);
});
Handlebars.registerHelper('if_eq', function(v1, v2, opts) {
    if(v1 == v2)
        return opts.fn(this);
    else
        return opts.inverse(this);
});
//点击切换样式
$('.item-btn').click(function(){
    $('.recharge').addClass('item-btn-active');
    $('.withdraw').removeClass('item-btn-active');
    $(this).parent().find('.item-btn').removeClass('item-btn-active');
    $(this).addClass('item-btn-active');
});
//插入充值模块
$('.recharge').click(function(){
    var th = $(this);
    $.ajax({
        url:"/assets/script/append-recharge.hbs",
        type:"get",
        async:false,
        success:function(data){
            $('.recharge-detail').remove();
            $('.withdraw-detail').remove();
            th.parent().parent().after(data);
            $('.coin-name').html(th.data('p'));
            var coinAddress = th.data('address');
            $('.address').val(coinAddress);
            $('.address').html(coinAddress);
            $('.copy').data('clipboard-text',$('.address').val());
            var coinId = th.data('id');
            //如果充值地址为空，显示另外一个地址
            if(!coinAddress){
                $.ajax({
                    url:'/recharge-get-address.json',
                    data:{coinId:coinId},
                    dataType:'json',
                    success:function(data){
                        $('.address').val(data.address);
                        var coinAddress = data.address;
                        //点击生成二维码
                        $('.ewm').click(function(){
                            $.ajax({
                                url:'/recharge-basecode.json',
                                data:{address:coinAddress},
                                type:'get',
                                dataType:'json',
                                contentType:'application/json;charset=UTF-8',
                                success:function(data){
                                    var imgAddress = data.baseCode;
                                    $('.ewm-img img').attr('src','data:image/jpeg;base64,'+imgAddress);
                                    $('.qrcode-modal').css('display','block');
                                },
                                error:function(msg){

                                }
                            });
                        });
                        $('.no').click(function(){
                            $('.qrcode-modal').css('display','none');
                        });
                    },
                    error:function (err) {

                    }
                })
            }
            //点击生成二维码
            $('.ewm').click(function(){
                $.ajax({
                    url:'/recharge-basecode.json',
                    data:{address:coinAddress},
                    type:'get',
                    dataType:'json',
                    contentType:'application/json;charset=UTF-8',
                    success:function(data){
                        var imgAddress = data.baseCode;
                        $('.ewm-img img').attr('src','data:image/jpeg;base64,'+imgAddress);
                        $('.qrcode-modal').css('display','block');
                    },
                    error:function(msg){

                    }
                });
            });
            $('.no').click(function(){
                $('.qrcode-modal').css('display','none');
            });
        }
    })
});
//插入提现模块
$('.withdraw').click(function(){
    var th = $(this);
    var coinId = th.data('id');
    $('.all-address').css('display','none');
    var isLogin = Number(th.data('login'));
    $('.google').change(function(){
        var google = Number($("#google").find("option:selected").val());
        console.log(google);
        if(google==1){
            $('.withdraw-btn').html('提交');
        }else{
            $('.withdraw-btn').html('发送验证码');
        }
    });

    var googleStatus = $('.withdraw').data('google');
    if(googleStatus==0){//两步验证设置
        $('.err-info').html('请去安全中心页面进行谷歌验证的设置');
    }
    if(isLogin == 0){
        alert('请去实名认证页面进行实名认证后再进行提现');
    } else{
//地址列表
        $.ajax({
            url:"/assets/script/address-list.hbs",
            type:"get",
            async:false,
            success:function(data){
                tpl = data;
            },
            error:function(err){

            }
        });
        var template = Handlebars.compile(tpl);
        $.ajax({
            url:'address-list.json',
            type:'get',
            dataType:'json',
            data:{
                coinId:coinId
            },
            success:function(data){
                var html = template(data);
                $('.all-address').append(html);
                $('.address-item').click(function(){
                    var address = $(this).find('.list-address').html();
                    $('.with-address').val(address);
                    $('.all-address').css('display','none');
                });

            },
            error:function(){

            }
        });
        //
        $.ajax({
            url:"/assets/script/append-withdraw.hbs",
            type:"get",
            async:false,
            success:function(data){
                $('.recharge-detail').remove();
                $('.withdraw-detail').remove();
                th.parent().parent().after(data);
                $('.with-coin-name').html(th.data('p'));
                $('.add').data('id',th.data('id'));
                $('.available').html(th.data('available'));
                $('.min').html(th.data('minbalance'));
                $('.dealRate').html(th.data('dealrate'));
                //全部提现
                $('.all-withdraw').click(function(){
                    var number = $('.available').html();
                    $('.number').val(number);
                });
                //新增
                $('.add').click(function(){
                    var coinId = $(this).data('id');
                    var remark = $('.note').val();
                    var address = $('.to-address').val();
                    var data = {
                        coinId:coinId,
                        remark:remark,
                        address:address
                    };
                    if(!address){
                        $('.err-info').html('请填写您的提币地址');
                    }else{
                        $.ajax({
                            url:'withdraw-address.json',
                            type:'post',
                            data:JSON.stringify(data),
                            dataType:'json',
                            contentType:'application/json;charset=UTF-8',
                            success:function(){
                                $('.err-info').html('添加成功！');
                                $('.with-address').val($('.to-address').val());
                            },
                            error:function(err){

                            }
                        })
                    }
                });
                //提现
                $('.withdraw-btn').click(function(){
                    var google = Number($(".google").find("option:selected").val());
                    var verifyType = google;
                    if(google==1){
                        $('.withdraw-btn').html('提交');
                    }else{
                        $('.withdraw-btn').html('发送验证码');
                    }
                    var idCardStatus = $.getAppData("idCardStatus");
                    var self = $(this);
                    var coinId = $('.add').data('id');
                    var balance = $('.number').val();
                    var address = $('.with-address').val();
                    console.log(self.parent().parent().prev().find('.withdraw').data('minbalance'));
                    var min = Number(self.parent().parent().prev().find('.withdraw').data('minbalance'));
                    var max = Number($('.withdraw').data('maxbalance'));
                    var dealRate =  Number($('.withdraw').data('dealrate'));
                    var googlePassword = $('.google-password').val();
                    var googleStatus = $('.withdraw').data('google');
                    // var min = minBalance*dealRate;
                    // var max = maxBalance*dealRate;
                    var data = {
                        coinId:coinId,
                        balance:balance,
                        address:address
                    };
                    var available = Number($('.available').html());
                    if(idCardStatus!==1){
                        $('.err-info').html('请先实名认证');
                    } else if(googleStatus==0){//两步验证设置
                        $('.err-info').html('请去安全中心页面进行谷歌验证的设置');
                    } else if(available < Number(balance)){//可用余额小于所提数量
                        $('.err-info').html('余额不足');
                    } else if(!address){
                        $('.err-info').html('请输入地址');
                    }else if(!coinId){
                        $('.err-info').html('请选择币种');
                    }else if(!balance){
                        $('.err-info').html('请输入提取数量');
                    }else if(Number(balance)<Number(min)){
                        $('.err-info').html('提现时请高于最低提现限额');
                    }else if(Number(balance)>Number(max)){
                        $('.err-info').html('提现时请低于最高提现限额');
                    } else{
                        if(google==2){  //手机验证

                        var html='<div class="verify-popup"><div class="title">短信验证码: </div> <div> <input class="verify-code" type="text"/> <button class="verify-code-btn">确定</button> </div></div>';
                        var initEvent=function (contentNode) {
                            $(".verify-code-btn").click(function(){
                                //发送验证码
                                var code= contentNode.find(".verify-code").val();
                                if(code==''){
                                    alert("验证码不能为空!");
                                }else{
                                    data.verifyCode=code;
                                    data.verifyType = google;
                                    $.ajax({
                                        url:'capital-withdraw.json',
                                        type:'post',
                                        data:JSON.stringify(data),
                                        dataType:'json',
                                        contentType:'application/json;charset=UTF-8',
                                        success:function(data){
                                            $('.err-info').html('提交成功！等待审核');
                                            model.hide();
                                            location.reload();
                                        },
                                        error:function(msg){
                                            if(data.match("^\{(.+:.+,*){1,}\}$")){
                                                $('.err-info').html(JSON.parse(msg.responseText).errors[0].msg);
                                                model.hide();
                                            }else{
                                                alert('请求不成功，请您刷新重试提现操作')
                                            }

                                        }
                                    })
                                }

                            });
                        };
                        var model= popup.init(html,null,null,initEvent);
                        //发送验证码
                        $.ajax({
                            url:'/withdraw-code.json',
                            type:'get',
                            contentType:'application/json;charset=UTF-8',
                            success:function(data){
                                $('.err-info').html('发送验证码成功！');
                                model.show();

                                //确认提现界面
                            },
                            error:function(msg){
                                $('.err-info').html(JSON.parse(msg.responseText).errors[0].msg);
                            }
                        });
                        }else{ //谷歌验证
                            var googleStatus = $('.withdraw').data('google');
                            if(googleStatus==0){//两步验证设置
                                $('.err-info').html('请去安全中心页面进行谷歌验证的设置');
                            }
                            if(!googlePassword){
                                $('.err-info').html('请填写谷歌验证码');
                            }
                            var secretCode = $('.google-password').val();
                            var reg=/^\d{6}$/;
                            if(reg.test(secretCode)){ //是6位数字
                                $.ajax({
                                    url:'/user/bind_google_secret.json',
                                    type:'patch',
                                    dataType:'json',
                                    data:{
                                        secretCode:secretCode
                                    },
                                    success:function(data1){
                                        // $('.err-info').html('提交成功');
                                        var code= $('.google-password').val();
                                        data.verifyCode=code;
                                        data.verifyType = google;
                                        $.ajax({
                                            url:'capital-withdraw.json',
                                            type:'post',
                                            data:JSON.stringify(data),
                                            dataType:'json',
                                            contentType:'application/json;charset=UTF-8',
                                            success:function(data){
                                                $('.err-info').html('提交成功！等待审核');
                                                // model.hide();
                                                // location.reload();
                                            },
                                            error:function(msg){
                                                if(data.match("^\{(.+:.+,*){1,}\}$")){
                                                    $('.err-info').html(JSON.parse(msg.responseText).errors[0].msg);
                                                    model.hide();
                                                }else{
                                                    alert('请求不成功，请您刷新重试提现操作')
                                                }
                                            }
                                        })
                                    },
                                    error:function(msg){
                                        $('.err-info').html(JSON.parse(msg.responseText).errors[0].msg);
                                    }
                                });
                            }else{
                                $('.err-info').html('请输入验证器上的6位数字密码');
                            }
                        }

                    }
                });
                //鼠标聚焦显示地址和备注列表
                $('.with-address').focus(function(){
                    $('.all-address').css('display','block');
                });
                $('.with-address').blur(function(){
                    setTimeout(function () {
                        $('.all-address').css('display','none');
                    }, 500);
                });
            }
        })
    }
});






